package cn.jbolt.admin.qiniu;

import cn.jbolt.core.api.JBoltApiBaseController;
import cn.jbolt.core.api.httpmethod.JBoltHttpGet;
import cn.jbolt.core.api.httpmethod.JBoltHttpPost;
import cn.jbolt.core.model.QiniuBucket;
import cn.jbolt.core.model.base.JBoltBaseModel;
import cn.jbolt.core.permission.CheckPermission;
import cn.jbolt.core.permission.OnlySaasPlatform;
import cn.jbolt.core.permission.UnCheckIfSystemAdmin;
import cn.jbolt.core.service.JBoltQiniuBucketService;
import cn.jbolt.core.service.base.JBoltBaseService;
import cn.jbolt.extend.permission.PermissionKey;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.core.paragetter.Para;
import com.jfinal.kit.Okv;
import com.jfinal.plugin.activerecord.tx.Tx;

@UnCheckIfSystemAdmin
@OnlySaasPlatform
@CheckPermission(PermissionKey.QINIU_BUCKET)
@Path("/api/admin/qiniuBucket")
public class QiniuBucketAdminController extends JBoltApiBaseController {

    @Inject
    private JBoltQiniuBucketService service;

    @Override
    protected JBoltBaseService<? extends JBoltBaseModel<?>> getModelService() {
        return service;
    }
    /**
     * 单个详情
     */
    @JBoltHttpGet
    public void index() {
        renderJBoltApiRet(findModelById());
    }

    /**
     * 从线上同步所有账号的bucket
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void syncAllQiniuBuckets() {
        renderJBoltApiRet(service.syncAllQiniuBuckets());
    }
    /**
     * 从线上同步bucket
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void syncFromQiniu() {
        renderJBoltApiRet(service.syncFromQiniu(getLong(0)));
    }

    /**
     * 分页条件查询
     * @param page
     * @param pageSize
     * @param keywords
     * @param qiniuId
     * @param zone
     */
    @JBoltHttpGet
    public void datas(@Para(value = "page",defaultValue = "1")Integer page,
                      @Para(value = "pageSize",defaultValue = "15")Integer pageSize,
                      @Para("keywords")String keywords,
                      @Para("qiniuId")Long qiniuId,
                      @Para("enable") Boolean enable,
                      @Para("zone")String zone) {
        renderJBoltApiSuccessWithData(service.paginateAdminDatas(page,pageSize,keywords, enable, qiniuId,zone));
    }

    /**
     * options数据源
     */
    @JBoltHttpGet
    public void options() {
        Long qiniuId = getLong(0);
        if(notOk(qiniuId)){
            renderJBoltApiSuccess();
            return;
        }
        renderJBoltApiSuccessWithData(service.getOptionList("name","sn", Okv.by("qiniu_id",qiniuId)));
    }


    /**
     * 更新
     */
    @Before(Tx.class)
    @JBoltHttpPost
    public void update(@Para("")QiniuBucket qiniuBucket) {
        renderJBoltApiRet(service.update(qiniuBucket));
    }

    /**
     * 切换toggleIsDefault
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void toggleIsDefault() {
        renderJBoltApiRet(service.toggleIsDefault(getId()));
    }

    /**
     * 切换启用状态
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void toggleEnable() {
        renderJBoltApiRet(service.toggleEnable(getId()));
    }
}
